﻿Maiks_GR = LibStub("AceAddon-3.0"):NewAddon("Maiks_GR", "AceConsole-3.0", "AceEvent-3.0")
Maiks_GR_LevelDelay = 9999999;
Maiks_GR_MinLevel = 5
Maiks_GR_MaxLevel = 85
Maiks_GR_Start = false
Maiks_GR_Message = "Hello $N. Varians Mercenaries is a level 10 guild who are looking for levelling members. Are you interested in joining? :)"
Maiks_GR_count = 1
Maiks_GR_Channel = false

function Maiks_GR:OnInitialize()
    Maiks_GR:Defaults()
    self.db = LibStub("AceDB-3.0"):New("Maiks_GRdb", defaults, "Default")
    LibStub("AceConfig-3.0"):RegisterOptionsTable("Maiks_GR", createoptions())
    self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Maiks_GR", "Maiks: GR", "Maiks")
    Maiks_GR:RegisterEvent('WHO_LIST_UPDATE')
    Maiks_GR:RegisterEvent('CHAT_MSG_CHANNEL')
    
    Maiks_GR_LevelDelay = Maiks_GRdb.LevelDelay
    Maiks_GR_MinLevel = Maiks_GRdb.MinLevel
    Maiks_GR_MaxLevel = Maiks_GRdb.MaxLevel
    Maiks_GR_Message = Maiks_GRdb.Message
end

function ActionButton_OnUpdate()
    if (Maiks_GR_LastTime == nil) then
      Maiks_GR_LastTime = GetTime()
      Maiks_GR_Channel_LastTime = GetTime()
      Maiks_GR_index = Maiks_GR_MinLevel
    else
        if Maiks_GR_Start then
            if  (GetTime() >= Maiks_GR_LastTime + Maiks_GR_LevelDelay) then
                Maiks_GR_LastTime = GetTime()
                if Maiks_GR_index >= Maiks_GR_MaxLevel + 1 then
                    if not Maiks_GRdb.Repeat then 
                        Maiks_GR_Start = false 
                        print("Maiks: GR Done.\nUse /maiks gr start - To recruit more people.")
                    end
                    Maiks_GR_index = Maiks_GR_MinLevel
                    
                else
                    SendWho(Maiks_GR_index)
                    --Maiks_GR:check(Maiks_GR_index)
                    --Maiks_GR_index = Maiks_GR_index + 1
                end
            end
        end
        if Maiks_GR_Channel then
            if  (GetTime() >= Maiks_GR_Channel_LastTime + (Maiks_GRdb.Channel.Minutes * 60)) then
                Maiks_GR_Channel_LastTime = GetTime()
                Maiks_GR:SendChannel()
            end
        end    
    end
end

function Maiks_GR:WHO_LIST_UPDATE()
    if Maiks_GR_Start then
        Maiks_GR:check(Maiks_GR_index)
        Maiks_GR_index = Maiks_GR_index + 1
    end
end

function Maiks_GR:check(Maiks_GR_index)
    for x=1,GetNumWhoResults() do
        local whispername, guild, level, race, class, zone, classFileName = GetWhoInfo(x)
        if guild == "" then
            if tContains(Maiks_GRdb.NameList, whispername) then else 
                tinsert(Maiks_GRdb.NameList, whispername)
                Guild = GetGuildInfo("player")
                local Message1 = string.gsub(Maiks_GRdb.Message, "$N", whispername) 
                local Message2 = string.gsub(Message1, "$G", Guild) 
                SendChatMessage(Message2, "WHISPER", "Common", whispername)
            end
        end
    end
end

function Maiks_GR:Defaults()
if Maiks_GRdb == nil then Maiks_GRdb = {} end
if Maiks_GRdb.LevelDelay == nil then Maiks_GRdb.LevelDelay = 10 end
if Maiks_GRdb.MinLevel == nil then Maiks_GRdb.MinLevel = 5 end
if Maiks_GRdb.MaxLevel == nil then Maiks_GRdb.MaxLevel = 85 end
if Maiks_GRdb.Message == nil then Maiks_GRdb.Message = "Would you like to join our guild?" end
if Maiks_GRdb.NameList == nil then Maiks_GRdb.NameList = {} end
if Maiks_GRdb.Repeat == nil then Maiks_GRdb.Repeat = false end
if Maiks_GRdb.Channel == nil then Maiks_GRdb.Channel = {} end
if Maiks_GRdb.Channel.Minutes == nil then Maiks_GRdb.Channel.Minutes = 60 end
if Maiks_GRdb.Channel.msg1frq == nil then Maiks_GRdb.Channel.msg1frq = 0 end
if Maiks_GRdb.Channel.msg2frq == nil then Maiks_GRdb.Channel.msg2frq = 0 end
if Maiks_GRdb.Channel.msg3frq == nil then Maiks_GRdb.Channel.msg3frq = 0 end
if Maiks_GRdb.Channel.msg4frq == nil then Maiks_GRdb.Channel.msg4frq = 0 end
if Maiks_GRdb.Channel.msg5frq == nil then Maiks_GRdb.Channel.msg5frq = 0 end
if Maiks_GRdb.Channel.msg6frq == nil then Maiks_GRdb.Channel.msg6frq = 0 end
if Maiks_GRdb.Channel.msg7frq == nil then Maiks_GRdb.Channel.msg7frq = 0 end
if Maiks_GRdb.Channel.msg8frq == nil then Maiks_GRdb.Channel.msg8frq = 0 end
if Maiks_GRdb.Channel.msg9frq == nil then Maiks_GRdb.Channel.msg9frq = 0 end
if Maiks_GRdb.Channel.msg10frq == nil then Maiks_GRdb.Channel.msg10frq = 0 end
if Maiks_GRdb.Channel.Message1 == nil then Maiks_GRdb.Channel.Message1 = "" end
if Maiks_GRdb.Channel.Message2 == nil then Maiks_GRdb.Channel.Message2 = "" end
if Maiks_GRdb.Channel.Message3 == nil then Maiks_GRdb.Channel.Message3 = "" end
if Maiks_GRdb.Channel.Message4 == nil then Maiks_GRdb.Channel.Message4 = "" end
if Maiks_GRdb.Channel.Message5 == nil then Maiks_GRdb.Channel.Message5 = "" end
if Maiks_GRdb.Channel.Message6 == nil then Maiks_GRdb.Channel.Message6 = "" end
if Maiks_GRdb.Channel.Message7 == nil then Maiks_GRdb.Channel.Message7 = "" end
if Maiks_GRdb.Channel.Message8 == nil then Maiks_GRdb.Channel.Message8 = "" end
if Maiks_GRdb.Channel.Message9 == nil then Maiks_GRdb.Channel.Message9 = "" end
if Maiks_GRdb.Channel.Message10 == nil then Maiks_GRdb.Channel.Message10 = "" end
if Maiks_GRdb.Channel.NumberOfMessages == nil then Maiks_GRdb.Channel.NumberOfMessages = 0 end
end

function createoptions()

    options = {
        name = "Maiks: Guild Recruitment",
        handler = Maiks_GR,
        type = "group",
        childGroups = "tab",
        args = {
            whispergroup= {
                name = "Whisper",
                type = "group",
                args = {
                    desc1 = {
                        name = "Whisper options for Maiks: GR",
                        type = "description",
                        order = 1,
                    },
                    minlevel = {
                        name = "Minimal Level",
                        type = "range",
                        order = 2,
                        width = "normal",
                        min = 1,
                        max = 85,
                        step = 1,
                        get = function() Maiks_GRdb.MinLevel = Maiks_GR_MinLevel return Maiks_GRdb.MinLevel end,
                        set = function(info, val) Maiks_GR_MinLevel = val Maiks_GRdb.MinLevel = val end,
                    },
                    maxlevel = {
                        name = "Maximal Level",
                        type = "range",
                        order = 3,
                        width = "normal",
                        min = 1,
                        max = 85,
                        step = 1,
                        get = function() Maiks_GRdb.MaxLevel = Maiks_GR_MaxLevel return Maiks_GRdb.MaxLevel end,
                        set = function(info, val) Maiks_GR_MaxLevel = val Maiks_GRdb.MaxLevel = val end,
                    },
                    Message = {
                        name = "Whisper Message. Use $N for player name and $G for guild name.",
                        type = "input",
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" end end,
                        order = 4,
                        width = "double",
                        get = function() Maiks_GRdb.Message = Maiks_GR_Message return Maiks_GRdb.Message end,
                        set = function(info, val) Maiks_GR_Message = val Maiks_GRdb.Message = val end,
                    },
                    delay = {
                        name = 'Time between whispering a level.',
                        type = "range",
                        order = 5,
                        width = "double",
                        min = 10,
                        max = 150,
                        step = 1,
                        get = function() Maiks_GRdb.LevelDelay = Maiks_GR_LevelDelay return Maiks_GRdb.LevelDelay end,
                        set = function(info, val) Maiks_GR_LevelDelay = val Maiks_GRdb.LevelDelay = val end,
                    },
                    repeater = {
                        name = "Repeat",
                        type = "toggle",
                        order = 6,
                        get = function() return Maiks_GRdb.Repeat end,
                        set = function(info, val) Maiks_GRdb.Repeat = val end,
                    },
                },
            },
            Channelgroup = {
                name = "Channels",
                type = "group",
                args = {
                    desc = {
                        name = "Channel options for Maiks: GR",
                        type = "description",
                        order = 1.2,
                    },
                    Channel = {
                        name = "On/Off",
                        type = "toggle",
                        order = 1.1,
                        get = function() return Maiks_GR_Channel end,
                        set = function(info, val) Maiks_GR_Channel = val end,
                    },
                    msg1 = {
                        name = "Message 1",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 2,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message1 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message1 = val end,
                    },
                    msg1frq = {
                        name = "Priority",
                        type = "range",
                        order = 3,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg1frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg1frq = val end,
                    },
                    msg2 = {
                        name = "Message 2",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 4,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message2 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message2 = val end,
                    },
                    msg2frq = {
                        name = "Priority",
                        type = "range",
                        order = 5,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg2frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg2frq = val end,
                    },
                    msg3 = {
                        name = "Message 3",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 6,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message3 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message3 = val end,
                    },
                    msg3frq = {
                        name = "Priority",
                        type = "range",
                        order = 7,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg3frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg3frq = val end,
                    },
                    msg4 = {
                        name = "Message 4",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 8,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message4 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message4 = val end,
                    },
                    msg4frq = {
                        name = "Priority",
                        type = "range",
                        order = 9,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg4frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg4frq = val end,
                    },
                    msg5 = {
                        name = "Message 5",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 10,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message5 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message5 = val end,
                    },
                    msg5frq = {
                        name = "Priority",
                        type = "range",
                        order = 11,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg5frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg5frq = val end,
                    },
                    msg6 = {
                        name = "Message 6",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 12,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message6 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message6 = val end,
                    },
                    msg6frq = {
                        name = "Priority",
                        type = "range",
                        order = 13,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg6frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg6frq = val end,
                    },
                    msg7 = {
                        name = "Message 7",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 14,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message7 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message7 = val end,
                    },
                    msg7frq = {
                        name = "Priority",
                        type = "range",
                        order = 15,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg7frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg7frq = val end,
                    },
                    msg8 = {
                        name = "Message 8",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 16,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message8 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message8 = val end,
                    },
                    msg8frq = {
                        name = "Priority",
                        type = "range",
                        order = 17,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg8frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg8frq = val end,
                    },
                    msg9 = {
                        name = "Message 9",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 18,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message9 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message9 = val end,
                    },
                    msg9frq = {
                        name = "Priority",
                        type = "range",
                        order = 19,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg9frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg9frq = val end,
                    },
                    msg10 = {
                        name = "Message 10",
                        type = "input",
                        multiline = true,
                        validate = function(info, val) if strlen(val) >= 254 then return "This string is longer then 254 characters" else return true end end,
                        order = 20,
                        width = "double",
                        get = function() return Maiks_GRdb.Channel.Message10 end,
                        set = function(info, val) Maiks_GRdb.Channel.Message10 = val end,
                    },
                    msg10frq = {
                        name = "Priority",
                        type = "range",
                        order = 21,
                        width = "normal",
                        min = 0,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.msg10frq end,
                        set = function(info, val) Maiks_GRdb.Channel.msg10frq = val end,
                    },
                    desc2 = {
                        name = "Send conditions",
                        type = "description",
                        order = 22,
                    },
                    Minutes = {
                        name = "Minutes",
                        type = "range",
                        order = 23,
                        width = "full",
                        min = 1,
                        max = 180,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.Minutes end,
                        set = function(info, val) Maiks_GRdb.Channel.Minutes = val end,
                    },
                    NumberOfMessages = {
                        name = "Number of messages between recruiting message",
                        type = "range",
                        order = 24,
                        width = "normal",
                        min = 1,
                        max = 100,
                        step = 1,
                        get = function() return Maiks_GRdb.Channel.NumberOfMessages end,
                        set = function(info, val) Maiks_GRdb.Channel.NumberOfMessages = val end,
                    },
                },
            },
        },
    }
    return options
end

function Maiks_GR:CHAT_MSG_CHANNEL(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)
    if arg8 == 2 then 
        Maiks_GR_count = Maiks_GR_count + 1
    end
    return Maiks_GR_count
end

function Maiks_GR:SendChannel()
    local priocount = Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq + Maiks_GRdb.Channel.msg4frq + Maiks_GRdb.Channel.msg5frq + Maiks_GRdb.Channel.msg6frq + Maiks_GRdb.Channel.msg7frq + Maiks_GRdb.Channel.msg8frq + Maiks_GRdb.Channel.msg9frq + Maiks_GRdb.Channel.msg10frq
    if Maiks_GR_count >= Maiks_GRdb.Channel.NumberOfMessages then
        priorandom = random(1, priocount)
        if priocount == 0 then 
        elseif Maiks_GRdb.Channel.msg1frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message1 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message2 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message3 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq + Maiks_GRdb.Channel.msg4frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message4 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq + Maiks_GRdb.Channel.msg4frq + Maiks_GRdb.Channel.msg5frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message5 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq + Maiks_GRdb.Channel.msg4frq + Maiks_GRdb.Channel.msg5frq + Maiks_GRdb.Channel.msg6frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message6 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq + Maiks_GRdb.Channel.msg4frq + Maiks_GRdb.Channel.msg5frq + Maiks_GRdb.Channel.msg6frq + Maiks_GRdb.Channel.msg7frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message7 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq + Maiks_GRdb.Channel.msg4frq + Maiks_GRdb.Channel.msg5frq + Maiks_GRdb.Channel.msg6frq + Maiks_GRdb.Channel.msg7frq + Maiks_GRdb.Channel.msg8frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message8 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq + Maiks_GRdb.Channel.msg4frq + Maiks_GRdb.Channel.msg5frq + Maiks_GRdb.Channel.msg6frq + Maiks_GRdb.Channel.msg7frq + Maiks_GRdb.Channel.msg8frq + Maiks_GRdb.Channel.msg9frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message9 , "CHANNEL", nil, "2")
        elseif Maiks_GRdb.Channel.msg1frq + Maiks_GRdb.Channel.msg2frq + Maiks_GRdb.Channel.msg3frq + Maiks_GRdb.Channel.msg4frq + Maiks_GRdb.Channel.msg5frq + Maiks_GRdb.Channel.msg6frq + Maiks_GRdb.Channel.msg7frq + Maiks_GRdb.Channel.msg8frq + Maiks_GRdb.Channel.msg9frq + Maiks_GRdb.Channel.msg10frq >= priorandom then 
            SendChatMessage(Maiks_GRdb.Channel.Message10 , "CHANNEL", nil, "2")
        end
        Maiks_GR_count = 0
    end
end